const { DataTypes } = require('sequelize')
const sequlize = require('./db')
const { hash } = require('../utils/hash')

//define(表名,定义列)

const Accounts = sequlize.define(
  'Account',
  {
    name: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        is: /^.{3,20}$/,
      },
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false,
      set(value) {
        this.setDataValue('password', hash(value))
      },
    },
    //0为用户,1为交管,2为管理员
    juri: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
  },
  {
    paranoid: true, //开启软删除
    tableName: 'Accounts', // 显式指定表名为 Accounts
  }
)
module.exports = Accounts
